<template>
  <div>
    <el-form inline :model="queryData" ref="queryData" class="query-form">
      <el-form-item label="加盟商名称:" prop="franchName">
        <el-input v-model="queryData.franchName" placeholder="请输入" style="width: 200px" clearable
                  @clear="formClear('franchName')"/>
      </el-form-item>
      <el-form-item label="加盟商等级:" prop="leval">
        <el-select v-model="queryData.leval" placeholder="请选择" clearable style="width: 200px"
                   @clear="formClear('leval')">
          <el-option label="白金" :value="0"></el-option>
          <el-option label="钻石" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="状态:" prop="state">
        <el-select v-model="queryData.status" placeholder="请选择" clearable style="width: 200px"
                   @clear="formClear('status')">
          <el-option label="正常" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="onSubmit">查 询</el-button>
        <el-button @click="onReset">重 置</el-button>
        <el-button type="primary" @click="lookOverEvent(null)">新 增</el-button>
      </el-form-item>
    </el-form>
    <el-table :data="tableList" border style="width: 100%;">
      <el-table-column prop="id" label="加盟商ID" align="center" width="80"/>
      <el-table-column prop="franchName" label="加盟商名称" align="center"/>
      <el-table-column prop="levalSleval" label="加盟商等级" align="center"/>
      <el-table-column prop="contactPerson" label="加盟商联系人" align="center"/>
      <el-table-column prop="phone" label="加盟商联系方式" align="center"/>
      <el-table-column prop="city" label="城市" align="center"/>
      <el-table-column label="状态" align="center" width="150">
        <template slot-scope="{row}">
          <el-switch :value="row.status" active-value="0" inactive-value="1" active-text="正常" inactive-text="停用" @change="switchChange($event, row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" fixed="right">
        <template slot-scope="{row}">
          <el-button type="text" @click="lookOverEvent(row)">查 看</el-button>
        </template>
      </el-table-column>
    </el-table>
    <Paging v-model="queryData" @change="getRequest"></Paging>
    <el-drawer title="加盟商信息" :visible.sync="drawer" direction="rtl" :before-close="drawerClose" size="500px"
               destroy-on-close class="drawer-css">
      <AllianceBusinessForm :entity="entity" v-if="drawer" @close="drawerClose" @refresh="getRequest"></AllianceBusinessForm>
    </el-drawer>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'

  export default {
    computed: {
      ...mapGetters(['confirm'])
    },
    components: {
      AllianceBusinessForm: () => import('./AllianceBusinessForm.vue')
    },
    name: 'allianceBusiness',
    data() {
      return {
        tableList: [],
        queryData: {
          franchName: null, // 加盟商名称
          leval: null, // 加盟商等级
          status: null, // 加盟商状态
          pageNum: 1,
          pageSize: 10,
          total: 0
        },
        entity: null,
        drawer: false
      }
    },
    beforeMount() {
      this.getRequest()
    },
    methods: {
      async switchChange (event, row) {
        await this.$ajax.get(`/sys/back/franchisee/status?id=${row.id}&status${event}`)
        this.getRequest()
      },
      /**
       * 新增 or 查看按钮事件
       */
      lookOverEvent(row) {
        this.entity = row ? JSON.parse(JSON.stringify(row)) : null
        this.drawer = true
      },
      /**
       * 关闭抽屉
       */
      drawerClose() {
        this.drawer = false
        this.entity = null
      },
      /**
       * 重置事件
       */
      onReset() {
        this.queryData.pageNum = 1
        this.queryData.province = null
        this.queryData.city = null
        this.queryData.county = null
        this.$refs['queryData'].resetFields()
        this.getRequest()
      },
      /**
       * 查询条件提交事件
       */
      onSubmit() {
        this.queryData.pageNum = 1
        this.getRequest()
      },
      /**
       * 列表请求事件
       */
      async getRequest() {
        const params = JSON.parse(JSON.stringify(this.queryData))
        const res = await this.$ajax.get('/sys/back/franchisee/list', { params })
        this.queryData.total = res.total
        this.tableList = res.rows
      },
      /**
       * form-input 置空事件
       */
      formClear(key) {
        this.queryData[key] = null
        this.onSubmit()
      }
    }
  }
</script>

<style scoped>

</style>
